import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import {Switch,Image, Tag} from 'ant-design-vue';
import { goodUpApi,goodDownApi,categoryAllApi} from '@/api/sys/shop';
import {uploadApi, uploadImgApi} from '@/api/sys/upload';
import { useMessage } from '@/hooks/web/useMessage';

type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '商品名称',
    dataIndex: 'goodName',
    // width: 200,
  },
  {
    title: '积分',
    dataIndex: 'integral',
    width: 100,
  },
  {
    title: '库存',
    dataIndex: 'stock',
    width: 100,
  },
  {
    title: '图片',
    dataIndex: 'mainImg',
    width: 180,
      customRender: ({ record }) => {

        let img = "";
        if(record.mainImg){
          let split = record.mainImg.split(",");
          img=split[0];
        }
        return h(Image, {
          "width":50,
          "height":50,
          "src":img
        } );
      },
  },
  {
    title: '分类',
    dataIndex: 'categoryName',
    width: 100,
  },

  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '启用',
        unCheckedChildren: '停用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          let data={goodId:record.goodId};

          if(record.status === 1){
            goodDownApi(data).then(() => {
              record.status = 0;
              createMessage.success(`已成功禁用`);
            })
              .catch(() => {
                createMessage.error('修改状态失败');
              })
              .finally(() => {
                record.pendingStatus = false;
              });
          }else{
            goodUpApi(data).then(() => {
              record.status = 1;
              createMessage.success(`已成功启用`);
            })
              .catch(() => {
                createMessage.error('修改状态失败');
              })
              .finally(() => {
                record.pendingStatus = false;
              });
          }
        },
      });
    },
  },

  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },

];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '商品名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '停用', value: 0 },
      ],
    },
    colProps: { span: 6 },
  },
  // {
  //   label: '班级',
  //   field: 'clazzId',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: clazzSelectsApi,
  //     resultField:"data",
  //     labelField: 'clazzName',
  //     valueField: 'clazzId',
  //   },
  //   colProps: { span: 6 },
  //
  // },
];

export const formSchema: FormSchema[] = [
  {
    field: 'goodName',
    label: '商品名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'integral',
    label: '积分',
    component: 'Input',
    required: true,
  },
  {
    field: 'stock',
    label: '库存',
    component: 'Input',
    required: true,
  },
  {
    label: '分类',
    field: 'categoryId',
    component: 'ApiSelect',
    componentProps: {
      api: categoryAllApi,
      // mode:"multiple",
      resultField:"data",
      labelField: 'categoryName',
      valueField: 'categoryId',

    },
    required: true,
  },
  // {
  //   field: 'mainImg',
  //   label: '商品图片',
  //   component: 'Input',
  // },
  {
    field: 'mainImgs',
    label: '商品图片',
    component: 'Upload',
    componentProps: {
      api:uploadImgApi,
      multiple:true,
      accept:['.png','.jpeg','.jpg'],
      resultField:"data.data",
      maxSize:"10",
      maxNumber:"30",
      previewDelete:function (res){
        console.log(res)
      },
      delete:function (record){
        console.log("--delete")
        console.log(record)
      },
      change:function (list){
        console.log("--change");
         console.log(list)

      }
    },
  },
  // {
  //   field: 'detail',
  //   label: '商品简介',
  //   component: 'Tinymce',
  // },
  {
      field: 'details',
      label: '商品简介',
    component: 'Upload',
    componentProps: {
      api:uploadImgApi,
      multiple:true,
      accept:['.png','.jpeg','.jpg'],
      resultField:"data.data",
      maxSize:"10",
      maxNumber:"30",
    },
  },


];
